package com.kj.xuyuan.mine;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kj.xuyuan.R;
import com.kj.xuyuan.adapter.ChargeHistoryAdapter;
import com.kj.xuyuan.base.BaseActivity;
import com.kj.xuyuan.base.BaseModel;
import com.kj.xuyuan.http.JsonCallback;
import com.kj.xuyuan.http.OkGoUtil;
import com.kj.xuyuan.http.UrlConstants;
import com.kj.xuyuan.model.ChargeBean;
import com.kj.xuyuan.model.ChargeHistoryBean;
import com.kj.xuyuan.util.LogUtils;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ConsumeHistoryActivity extends BaseActivity {

    @BindView(R.id.rv_history)
    RecyclerView rvHistory;
    @BindView(R.id.sml_history)
    SmartRefreshLayout smlHistory;
    @BindView(R.id.ll_no_data)
    LinearLayout llNoData;

    private List<ChargeHistoryBean> data;
    private int page = 1;
    private ChargeHistoryAdapter adapter;

    public static void actionStar(Context context){
        Intent intent = new Intent(context,ConsumeHistoryActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.activity_consume_history;
    }

    @Override
    protected void initView() {
        setTitleText("消费记录");
        initRecyclerView();
        getData();
    }

    private void getData() {
        Map map = new HashMap();
        map.put("page",page+"");
        OkGoUtil.postRequest(getContext(), UrlConstants.CHANGE_HISTORY, map, new JsonCallback<BaseModel<List<ChargeHistoryBean>>>() {
            @Override
            public void onSuccess(Response<BaseModel<List<ChargeHistoryBean>>> response) {
                if (response != null && response.body() != null) {
                    if(response.body().getCode() == 1){
                        if(page == 1){
                            data.clear();
                        }
                        data.addAll(response.body().getData());
                        adapter.notifyDataSetChanged();
                        llNoData.setVisibility(data.size() == 0 ? View.VISIBLE : View.GONE);
                        rvHistory.setVisibility(data.size() == 0 ? View.GONE : View.VISIBLE);
                    }
                }
                smlHistory.finishRefresh();
                smlHistory.finishLoadMore();
            }

            @Override
            public void onError(Response<BaseModel<List<ChargeHistoryBean>>> response) {
                super.onError(response);
                LogUtils.e(TAG,response.getException().getMessage());
                smlHistory.finishRefresh();
                smlHistory.finishLoadMore();
            }
        });
    }

    private void initRecyclerView() {
        data = new ArrayList<>();
        adapter = new ChargeHistoryAdapter();
        rvHistory.setLayoutManager(new LinearLayoutManager(context));
        rvHistory.setAdapter(adapter);
        adapter.setNewData(data);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getTitleBarType() {
        return 0;
    }
}